(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
//			initLeave(1,pageSize);
			getGradeAttence();
		});
	});
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#start-time").val(startTime+"00:00");
		$("#end-time").val(newDate);
	}
	function getGradeAttence(){
		processBegin("数据加载中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");
		var starttime = strToDate($("#start-time").val() + ":00");
		var endtime = strToDate($("#end-time").val() + ":00");
		var obj = {
				starttime:starttime+"",
				endtime:endtime+"",
				schoolcode:schoolcode,
				classcode:classcode
		};
		var data = {
				cmd:"queryAttendanceByTeacher",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = $.merge(res.data.list,res.data.total);
				if(list.length > 0){
					$("#grade-table").empty();
					$("#grade-table").html(_.template($("#data-template").html(),list));
					processEnd("加载成功");
					$(".at-body-ta td a").unbind("click");
					$(".at-body-ta td a").click(function(){
						$("#detail-show").show();
						$("#right-tables").hide();
						var studentcode = $(this).attr("studentcode");
						var ttype = $(this).attr("ttype");
						$("#detail-show").attr("src","detail.html?studentcode="+studentcode+"&type="+ttype);
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#grade-table").empty();
					$("#grade-table").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#grade-table").empty();
				$("#grade-table").append(content);
				processError("加载失败");
			}
		},null,null);	
	}
	function initLeave(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var obj = {
				starttime:starttime,
				endtime:endtime,
				schoolcode:schoolcode,
				currentpage:currentpage,
				pagesize:pagesize
		};
		if(classcode != "" && classcode != undefined){
			obj["classcode"] = classcode;
		}
		var data = {
				cmd:"getAttendanceByClasscodeByPage",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				if(data){
					var total = data[0].total;
					totalSize = Math.ceil(total/pagesize);
					if(totalSize <= 5 && totalSize >0){
						for(var i = totalSize-1 ; i >= 0; i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}else if(totalSize == 0){
						pagecontent += "<div class=\"pages-btn\">1</div>";
					}else if(totalSize > 5){
						var lastp = (now*5)+5;
						if(lastp > totalSize){
							lastp = totalSize;
						}
						for(var i = lastp-1 ; i >= (now*5); i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}
					pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
					$("#student-page").empty();
					$("#student-page").append(pagecontent);
					
					$(".pages-btn").unbind("click");
					$(".pages-btn").click(function(){
						$(".pages-btn").removeClass("page-btn-select");
						$(this).addClass("page-btn-select");
						currentPage = parseInt($(this).text());
						initAttence(currentPage,pageSize);
					});
					
					$(".pages-btn-last").unbind("click");
					$(".pages-btn-last").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(now > 0){
								now --;
								initAttence((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					});
					
					$(".pages-btn-next").unbind("click");
					$(".pages-btn-next").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(totalSize%5 == 0){
								if((now+1) < totalSize/5){
									now ++;
									initAttence((now*5+1),pageSize);
								}else{
									return false;
								}
							}else{
								if((now+1) <= (totalSize-totalSize%5)/5){
									now ++;
									initAttence((now*5+1),pageSize);
								}else{
									return false;
								}
							}
						}
					});
					if(data.length > 0){
						for(var i = 0; i < data.length; i++){
							for(var key in data[i]){
								if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
									data[i][key] = "";
								}
							}
							if(data[i].timetype == 1){
								data[i].timetype = "上午";
							}else if(data[i].timetype == 2){
								data[i].timetype = "下午";
							}
							var time = getFormatDateByLong(parseInt(data[i].time)*1000,"yyyy-MM-dd hh:mm");
							content += "<div class=\"table-trs\">" +
								"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
									"<div class=\"table-name\" style='width:120px;'>"+data[i].grade + "年级" + data[i].tclass + "班" + "</div>" +
									"<div class=\"table-code\" style='width:160px;'>"+ time + " " + data[i].timetype+"</div>" +
								"</div>" +
								"<div class=\"tables-middle\">" +
									"<div class=\"table-class\" style='width:150px;'>实到人数："+data[i].normalnum+"</div>" +
									"<div class=\"table-code\" style='width:150px;'>应到人数："+data[i].studentnum+"</div>" +
								"</div>" +
								"<div class=\"tables-right\">" +
									"<div class=\"table-address\">请假："+data[i].leavenum+"</div>" +
									"<div class=\"table-address\">缺勤："+data[i].abnormalnum+"</div>" +
								"</div>" +
							"</div>";
						}
						$("#attence-data-show").empty();
						$("#attence-data-show").append(content);
					}else{
						content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
									"</div>";
						$("#attence-data-show").empty();
						$("#attence-data-show").append(content);
					}
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#attence-data-show").empty();
					$("#attence-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#attence-data-show").empty();
				$("#attence-data-show").append(content);
			}
		},null,null);	
	}
	function deleteClass(classcode){
		
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function showData(data,type){
		if(type == "detail"){
			$("#attence-form-show").contents().find("#save-btns-content").hide();
		}else if(type == "edit"){
			$("#attence-form-show").contents().find("#save-btns-content").show();
		}
		for(var key in data){
			if($("#attence-form-show").contents().find("#attence-" + key)[0]){
				if($("#attence-form-show").contents().find("#attence-" + key)[0].tagName.toLocaleLowerCase() == "input"){
					$("#attence-form-show").contents().find("#attence-" + key).val(data[key]);
				}else if($("#attence-form-show").contents().find("#attence-" + key)[0].tagName.toLocaleLowerCase() == "select"){
					var len = $("#attence-form-show").contents().find("#attence-" + key+" option");
					for(var i = 0; i < len.length ; i++){
						if(len[i].value == data[key]){
							len[i].selected = true;
						}
					}
				}
			}
		}
	}
	function pagebtns(){
		$("#attence-search-btn").click(function(){
			now = 0;
			getGradeAttence();
//			initLeave(1,pageSize);
		});
		$(".classes-choice",parent.document).unbind("click");
		$(".classes-choice",parent.document).click(function(){
			$(".classes-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("classes-choice-select");
			now = 0;
			getGradeAttence();
//			initLeave(1,pageSize);
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();